package com.wenhao.pool;

import org.junit.jupiter.api.Test;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 * new ThreadPoolExecutor.AbortPolicy() 不处理，然后抛出异常
 * new ThreadPoolExecutor.CallerRunsPolicy() 哪里来的去哪里
 * new ThreadPoolExecutor.DiscardPolicy() 直接丢掉任务 不抛异常
 * new ThreadPoolExecutor.DiscardOldestPolicy() 尝试去和最老的竞争，不抛出异常
 *
 * @author Wenhao Tong
 * @create 2021-08-20 16:08
 */
public class ThreadPoolExcutorDemo {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );

        try {
            for (int i = 0;i < 10;i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " ok");
                });
            }
        } finally {
            threadPool.shutdown();
        }
    }

}
